<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/6/5
 * Time: 13:26
 */

namespace app\common\service;
use app\common\model\Archives;
use app\common\model\ArchivesType;

/**
 *
 * ArchivesService Service层
 * @package app\common\service
 */

class ArchivesService
{
    /**
     * 获取文档列表
     * @param $limit
     * @param $offset
     * @return false|static[]
     */
    public function getAllList($limit,$offset){
        $archivesList = Archives::all(function ($query) use ($limit,$offset){
            $query->where('is_del',Archives::NOT_DELETED)->limit($offset,$limit)->order('created','desc');
        },['type']);
        return $archivesList;
    }

    /**
     * 获取文章或项目具体内容
     * @param $id
     * @return null | Archives
     */
    public function get($id){

        $archives = Archives::get(['id'=>$id,'is_del'=>Archives::NOT_DELETED]);
        if($archives){
            $addon = $archives->type->type_tag;
            if(in_array($addon , ArchivesType::$allowType)){
                $returnArr = $archives->hidden(['is_del','type','extend_table','main_table'])->toArray();
                $returnArr['extend'] = $archives->$addon;
                return $returnArr;
            }else{
                return null;
            }
        }else{
            return null;
        }
    }
}